#include "terrainbrushactor.h"
#include <osg/Geode>
#include "transformable.h"
#include "datamanager.h"

namespace VrDataCore
{
	TerrainBrushActor::TerrainBrushActor():_collisionOn(false),_density(1)
	{
		setClassName("TerrainBrushActor");
	}

	TerrainBrushActor::~TerrainBrushActor()
	{}

	void TerrainBrushActor::buildPropertyMap()
	{}
	
	void TerrainBrushActor::createDrawable()
	{
		_drawable = new CoreBase::Transformable(this);
		setTraverseMask(NODE_MASK_NOSELECT);
		setInitialTraverseMask(NODE_MASK_NOSELECT);
		initialNode();
	}
	
	osg::ref_ptr<CoreBase::ActorBase> TerrainBrushActor::clone()
	{
		osg::ref_ptr<CoreBase::ActorBase> clone = DrawableActor::clone();
		return clone;
	}

	void TerrainBrushActor::initialNode()
	{
		_rectShape = new CoreBase::LineRectShape(false);
		_circleShape = new CoreBase::CircleShape(osg::Vec3(0,0,0),0.1f,32);
		_brushType = NONE;
		_brushShape = CIRCLE;
		_width = 8;
	}

	void TerrainBrushActor::onMapLoadEnd(CoreBase::Map* map)
	{}

	void TerrainBrushActor::setBrushType(BrushType type)
	{
		_brushType = type;
		_drawable->removeChild(_rectShape);
		_drawable->removeChild(_circleShape);
		switch (_brushType)
		{
		case VEGETATION:
		case CLEAR_VEGETATION:
			switch(_brushShape)
			{
			case RECT:
				_drawable->addChild(_rectShape);
				break;
			case CIRCLE:
				_drawable->addChild(_circleShape);
				break;
			default:
				break;
			}
			break;
		case NONE:
		default:
			break;
		}
	}

	TerrainBrushActor::BrushType TerrainBrushActor::getBrushType() const
	{
		return _brushType;
	}

	void TerrainBrushActor::setBrushShape(BrushShape shape)
	{
		_brushShape = shape;
		_drawable->removeChild(_rectShape);
		_drawable->removeChild(_circleShape);
		switch (_brushType)
		{
		case VEGETATION:
			switch(_brushShape)
			{
			case RECT:
				_drawable->addChild(_rectShape);
				break;
			case CIRCLE:
				_drawable->addChild(_circleShape);
				break;
			default:
				break;
			}
			break;
		case NONE:
		default:
			break;
		}
	}

	TerrainBrushActor::BrushShape TerrainBrushActor::getBrushShape() const
	{
		return _brushShape;
	}

	void TerrainBrushActor::setWidth(float width)
	{
		switch(_brushShape)
		{
		case RECT:
			_width = width*2.0f;
			_rectShape->updateSize(osg::Vec3(0,0,0),_width,_width,false);
			break;
		case CIRCLE:
			_width = width;
			_circleShape->setRadius(_width);
			break;
		default:
			break;
		}
	}

	float TerrainBrushActor::getWidth()
	{
		return _width;
	}

	void TerrainBrushActor::setDensity(int value)
	{
		_density = value;
	}

	int TerrainBrushActor::getDensity()
	{
		return _density;
	}

	void TerrainBrushActor::setPlantObjects(const QStringList& meshfilename,PlantType type,bool collision)
	{
		_plantObjects = meshfilename;
		_collisionOn = collision;
		_plantType = type;
	}

	TerrainBrushActor::PlantType TerrainBrushActor::getPlantType()
	{
		return _plantType;
	}

	QStringList TerrainBrushActor::getPlantObjects()
	{
		return _plantObjects;
	}

	bool TerrainBrushActor::getCollisionOn()
	{
		return _collisionOn;
	}
}